<?php
include './include/common.inc.php';
include './include/investigate.class.php';
include './include/questions.class.php';
include './include/option.class.php';
require '../../include/form.class.php';

$investigate_db = new investigate();
$questions_db = new questions();
$option_db = new option();
$action = isset( $action ) ? $action : 'manage';
$parent_forward = get_forward($forward);
$forward = get_forward( 'option.php' );


$investigate_list = $investigate_db->get_all();
$questions_list = $questions_db->get_all();

$params = array();
array_push( $params, "status = 99" );
if( isset( $investigateid ) && strlen( $investigateid ) > 0 )
{
    array_push( $params, "investigateid = '$investigateid'" );
}
if( isset( $questionsid ) && strlen( $questionsid ) > 0 )
{
    array_push( $params, "questionsid = '$questionsid'" );
}
if( isset( $optionname ) && strlen( $optionname ) > 0 )
{
    array_push( $params, "optionname like '%$optionname%'" );
}
$order = " ORDER BY listorder DESC, inputtime DESC";

if( $current_user === false )
{
	if( isset( $edufepm ) && $edufepm == 'iframe' )
	{
		$response->alert( '请您登陆后再访问' );
		$response->redirect( $base_dir . 'admin/login.php' );
		$response->execute();
	}
	else
	{
		showmessage( '请您登陆后再访问', $base_dir . 'admin/login.php' );
	}
}

switch( $action )
{
    case 'add':
        $have_perm = check_perms( 'option002' );
        if( isset( $_POST['optionname'] ) )
		{
            if( $have_perm === false )
			{
				$response->alert( '您没有权限进行此操作！' );
				$response->execute();
			}
			if( empty( $optionname ) || strlen( $optionname ) > 100 )
			{
				$response->text_alert( 'error','optionname','选项名称不能为空且长度不能大于100个字符' );
			}
			$response->real_execute();
			$params = array(
				'optionname'	 =>  $optionname,
                'investigateid'  =>  $investigateid,
                'questionsid'     =>  $questionsid,
                'status'         =>  99,
                'username'       =>  $_SESSION['current_user']['username'],
                'inputtime'      =>  time(),
			);
            $res = $option_db->add( $params );
			if( !empty($res) )
			{
				$response->alert( '添加成功' );
				$response->redirect( urldecode( $forward ) );
			}
			else
			{
				$response->alert( '添加失败' );
				$response->script( 'self.location = self.location' );
			}
			$response->execute();
		}
		else
		{
            if( $have_perm === false )
			{
				showmessage('您没有权限进行此操作！');
			}
			include template( 'admin', 'option_add' );
		}
        break;

    case 'edit':
        $have_perm = check_perms( 'option003' );
        if( isset( $_POST['optionname'] ) )
		{
            if( $have_perm === false )
			{
				$response->alert( '您没有权限进行此操作！' );
				$response->execute();
			}
			if( empty( $optionname ) || strlen( $optionname ) > 100 )
			{
				$response->text_alert( 'error','optionname','选项名称不能为空且长度不能大于100个字符' );
			}
			$response->real_execute();
            $params = array(
				'optionname'	 =>  $optionname,
                'investigateid'  =>  $investigateid,
                'questionsid'    =>  $questionsid,
			);
            $res = $option_db->edit( $params, $id );
			if( !empty($res) )
			{
				$response->alert( '修改成功' );
				$response->redirect( urldecode( $forward ) );
			}
			else
			{
				$response->alert( '修改失败' );
				$response->script( 'self.location = self.location' );
			}
			$response->execute();
		}
		else
		{
            if( $have_perm === false )
			{
				showmessage('您没有权限进行此操作！');
			}
            $info = $option_db->get( $id );
			if( !isset( $info['id'] ) )
			{
				showmessage( '要编辑的试题不存在' );
			}
			include template( 'admin', 'option_upd' );
		}
        break;

    case 'del':
        $have_perm = check_perms( 'option004' );
        if( $have_perm === false )
        {
            $response->alert( '您没有权限进行此操作！' );
            $response->execute();
        }
        $info = $option_db->get( $id );
		if( !isset( $info['id'] ) )
		{
			$response->alert( '要删除的试题不存在' );
			$response->execute();
		}
        $res = $option_db->del( $id );
		if( $res == false )
		{
			$response->alert( '删除失败' );
		}
		else
		{
			$response->alert( '删除成功' );
			$response->script( 'self.location=self.location' );
		}
		$response->execute();
        break;

    case 'del_all':
        $have_perm = check_perms( 'option004' );
        if( $have_perm === false )
        {
            $response->alert( '您没有权限进行此操作！' );
            $response->execute();
        }
        if( isset( $_POST['oid'] ) )
		{
			foreach ( $oid as $id )
			{
				$option_db->del( $id );
			}
			$response->alert( '删除成功！' );
			$response->script( 'self.location=self.location' );
			$response->execute();
		}
		else
		{
			$response->alert( '请选择要删除的试题' );
			$response->execute();
		}
        break;

    case 'listorder':
        $have_perm = check_perms( 'option005' );
        if( $have_perm === false )
        {
            $response->alert( '您没有权限进行此操作！' );
            $response->execute();
        }
        $result = $option_db->listorder( $listorder );
		if( $result )
		{
            $response->alert( '修改成功' );
            $response->redirect( urldecode( $forward ) );
		}
		else
		{
			$response->alert( '修改失败' );
		}
        $response->execute();
        break;

    case 'search':
        $url = 'option.php';
		$url .= isset( $optionname ) && !empty( $optionname ) ? '?optionname='.urlencode( $optionname ) : '';
		$response->redirect( $url );
		$response->execute();
        break;

    case 'manage':

    default:
        $have_perm = check_perms( 'option001' );
        if( $have_perm === false )
        {
            showmessage( '您没有权限进行此操作！' );
        }
        $page = isset( $page ) ? intval( $page ) : 1;
		$page = max( $page, 1 );
        $pagesize = 10;
		$option_list = $option_db->get_list( $params, $order, $page, $pagesize );
        $option_pages = $option_db->pages;
        $forward = make_forward();
		include template( 'admin', 'option_manage' );
        break;
}

?>